package cn.tedu.system.oa.starup;

import cn.tedu.system.oa.stamp.dao.repository.StampCacheRepository;
import cn.tedu.system.oa.stamp.pojo.entity.Stamp;
import cn.tedu.system.oa.stamp.service.StampService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
@Slf4j
public class CacheInit implements ApplicationRunner {

    @Autowired
    private StampService stampService;

    @Autowired
    private StampCacheRepository stampCacheRepository;

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        1.查询印章列表
        List<Stamp> list = checkStampList();

    }

    private List<Stamp>  checkStampList() {
        log.info("缓存预热:查询印章列表");
        List<Stamp> stampList =  stampCacheRepository.getAllStamp();
        log.debug("缓存预热:查询印章列表,结果:{}",stampList);
        if (CollectionUtils.isEmpty(stampList)) {
            log.debug("缓存预热:查询印章列表为空，从数据库查询存入:{}",stampList);
            List<Stamp> list = stampService.selectList(null);
            stampCacheRepository.saveAllStamp(list);
            log.debug("从数据库查询存入redis成功:{}",list);
            return list;
        }
        return stampList;
    }
}
